package com.baway.biliclitent.fragment.ZoneType;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.baway.biliclitent.R;
import com.baway.biliclitent.adapter.RegionTypeDetailsAdapter;
import com.baway.biliclitent.model.RegionBean;
import com.baway.biliclitent.util.OkHttpUtils;
import com.baway.biliclitent.util.UrlUtils;

/**
 * Created by dell on 2017/3/10.
 */

public class RegionTypeRecommendFragment extends Fragment {

    private RecyclerView recycler;
    private int index;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_region_recommend,container,false);
        recycler = (RecyclerView) view.findViewById(R.id.region_type_recycler);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
       //进行取值
        Bundle arguments = getArguments();
        index = (int) arguments.get("index");
        //进行网络获取
        getData();
    }
    /**
     *  网络请求方法
     */
    private void getData(){
        OkHttpUtils httpUtils = OkHttpUtils.getHttpUtils();
        String path = UrlUtils.PARTITION_HEAD+UrlUtils.PARTITION_TYPE+index;
        Log.i("TAG", "getData: 路径"+path);
        httpUtils.loadDataFromNet(path, RegionBean.class, new OkHttpUtils.CallBackListener<RegionBean>() {
            @Override
            public void onSuccess(RegionBean result) {
                RegionBean.DataBeanHeadline data = result.getData();
                //进行适配
                /*RegionTypeRecommendAdapter adapter = new RegionTypeRecommendAdapter(getActivity(),data);
                RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
                recycler.setLayoutManager(layoutManager);
                recycler.setAdapter(adapter);*/
                RegionTypeDetailsAdapter regionTypeRecommendAdapter = new RegionTypeDetailsAdapter(getActivity(),data.getNewX(),data.getRecommend());
                RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
                recycler.setLayoutManager(layoutManager);
                recycler.setAdapter(regionTypeRecommendAdapter);
                regionTypeRecommendAdapter.setOnClickChildListener(new RegionTypeDetailsAdapter.OnChildClickListener() {
                    @Override
                    public void onChildClick(RecyclerView recyclerView, View view, int position, String data) {
                        //进行页面的跳转
                        Intent intent = new Intent(getActivity(),RegionDesActivity.class);
                        //进行传值

                        startActivity(intent);
                    }
                });
            }

            @Override
            public void onFail() {

            }
        });
    }
    /**
     *  传值的方法
     */
    public static RegionTypeRecommendFragment setData(int num){
        RegionTypeRecommendFragment fragment = new RegionTypeRecommendFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("index",num);
        fragment.setArguments(bundle);
        return fragment;
    }
}
